package com.barrett.hvzmoderator.com.barrett;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.barrett.hvzmoderator.R;

class AchAdapter extends ArrayAdapter<Achievement> {

	private ArrayList<Achievement> items;
	
	public AchAdapter(Context context, int textViewResourceId, ArrayList<Achievement> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		// TODO Auto-generated constructor stub
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.itemach, null);
            }
            Achievement ach = items.get(position);
            if (ach != null) {
                    TextView name = (TextView) v.findViewById(R.id.itemName);
                    TextView points = (TextView) v.findViewById(R.id.itemPoints);
                    TextView obt = (TextView) v.findViewById(R.id.itemObtained);
                    ImageView pic = (ImageView) v.findViewById(R.id.itemPic);
                    if (name != null) {
                          name.setText(ach.name);                            }
                    if(points != null){
                          points.setText("Points: "+ ach.points);
                    }
                    if(pic != null) {
                    	getImage(ach.pic, pic);
                    }
                    if(obt != null){
                    	if(ach.obtained){
                    		obt.setText("Obtained");
                    		pic.setAlpha(255);
                    	}
                    	else {
                    		obt.setText("");
                    		pic.setAlpha(50);
                    	}
                    }
            }
            return v;
    }
	
	private void getImage (String url, ImageView i) {
        try {
                            /* Open a new URL and get the InputStream to load data from it. */
                            URL aURL = new URL(url);
                            URLConnection conn = aURL.openConnection();
                            conn.connect();
                            InputStream is = conn.getInputStream();
                            /* Buffered is always good for a performance plus. */
                            BufferedInputStream bis = new BufferedInputStream(is);
                            /* Decode url-data to a bitmap. */
                            Bitmap bm = BitmapFactory.decodeStream(bis);
                            bis.close();
                            is.close();
                            /* Apply the Bitmap to the ImageView that will be returned. */
                            bm.setDensity(80);
                            i.setImageBitmap(bm);
                    } catch (IOException e) {
                            i.setImageResource(R.drawable.icon);
                            Log.e("DEBUGTAG", "Remtoe Image Exception", e);
                    }
		
	}
	public void switchObt(int pos) {
		Achievement obj = items.get(pos);
		
		obj.setObtained(!obj.obtained);
		items.remove(pos);
		items.add(pos, obj);
		
	}
	

}
